package cn.edu.gzhmt.goods.admin.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.gzhmt.category.admin.service.IAdminCategoryService;
import cn.edu.gzhmt.category.admin.service.impl.AdminCategoryServiceImpl;
import cn.edu.gzhmt.category.domain.Category;
import cn.edu.gzhmt.goods.admin.service.IAdminGoodsService;
import cn.edu.gzhmt.goods.admin.service.impl.AdminGoodsServiceImpl;
import cn.edu.gzhmt.goods.domain.Goods;
import cn.edu.gzhmt.goods.domain.SortGoods;
import cn.edu.gzhmt.utils.commons.CommonUtils;
import cn.edu.gzhmt.utils.web.servlet.BaseServlet;

/**
 * 后台管理商品管理模块：web层
 * @author azhuo
 *
 */
public class AdminGoodsServlet extends BaseServlet {

	private IAdminGoodsService adminGoodsService = new AdminGoodsServiceImpl();
	private IAdminCategoryService adminCategoryService = new AdminCategoryServiceImpl();

	/**
	 * 查询所有商品
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAllGoods(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		List<Goods> goodsList = adminGoodsService.findAllGoods();
		request.setAttribute("goodsList", goodsList);

		return "f:/adminjsps/goods/findGoods.jsp";
	}

	/**
	 * 加载商品
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String loadGoods(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String gid = request.getParameter("gid");

		Goods goods = adminGoodsService.loadGoods(gid);
		request.setAttribute("goods", goods);

		return "f:/adminjsps/goods/alterGoods.jsp";
	}

	/**
	 * 删除商品
	 * 根据id删除
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String removeGoods(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String gid = request.getParameter("gid");

		// 删除商品
		adminGoodsService.removeGoods(gid);

		response.getWriter().print(gid);

		return null;
	}

	/**
	 * 多条件组合查询商品
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findGoods(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Goods form = CommonUtils.toBean(request.getParameterMap(), Goods.class);

		String cname = request.getParameter("cname");
		if (cname != null) {
			Category category = adminCategoryService.loadCategoryByName(cname);
			form.setCategory(category);
		}
		List<Goods> goodsList = adminGoodsService.findGoods(form);

		request.setAttribute("goodsList", goodsList);

		return "f:/adminjsps/goods/findGoods.jsp";
	}
	
	/**
	 * 商品销售排行
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String sellingOrderSortGoods(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		// 商品销售排行
		List<SortGoods> sortGoodsList = adminGoodsService.sellingOrderSortGoods();
		request.setAttribute("sortGoodsList", sortGoodsList);
		
		return "f:/adminjsps/statistics/sortGoods.jsp";
	}

}
